package org.ruanjj.majorityelement;

import java.util.Arrays;

public class Solution1 {
    public int majorityElement(int[] nums) {
       nums = Arrays.stream(nums).sorted().toArray();
       int n = nums.length;
       int maxCount = 0;
       int count = 1;
       int res = nums[0];
       int i = 1;
       while(i < n){
           if(nums[i-1] != nums[i]){
               if(count >= maxCount){
                   res = nums[i-1];
                   maxCount = count;
               }
               count = 1;
           }else{
               count++;
               if(i == n-1){
                   if(count >= maxCount){
                       res = nums[i];
                       maxCount = count;
                   }
               }
           }
           i++;
       }
       return res;
    }
}
